import { defineStore } from 'pinia'

/**
 * 饮食状态管理
 * 存储用户饮食计划、食物记录等
 */
export const useDietStore = defineStore('diet', {
  state: () => ({
    // 当日摄入卡路里
    caloriesIntake: 1462,
    
    // 目标卡路里
    caloriesTarget: 2100,
    
    // 营养摄入
    nutrition: {
      protein: 42, // 蛋白质（克）
      fat: 55,     // 脂肪（克）
      carbs: 120    // 碳水（克）
    },
    
    // 今日餐食记录
    meals: {
      breakfast: [
        {
          id: 1,
          name: '蔬果沙拉',
          calories: 180,
          preparation: '5分钟',
          image: 'https://images.unsplash.com/photo-1494390248081-4e521a5940db?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
        },
        {
          id: 2,
          name: '酸奶麦片',
          calories: 240,
          preparation: '3分钟',
          image: 'https://images.unsplash.com/photo-1525385133512-2f3bdd039054?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
        }
      ],
      lunch: [
        {
          id: 3,
          name: '鸡胸肉沙拉',
          calories: 320,
          preparation: '15分钟',
          image: 'https://images.unsplash.com/photo-1546069901-ba9599a7e63c?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
        },
        {
          id: 4,
          name: '牛油果吐司',
          calories: 330,
          preparation: '10分钟',
          image: 'https://images.unsplash.com/photo-1573333515743-56d57731dd1f?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
        }
      ],
      dinner: [
        {
          id: 5,
          name: '烤三文鱼',
          calories: 220,
          preparation: '25分钟',
          image: 'https://images.unsplash.com/photo-1467003909585-2f8a72700288?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
        },
        {
          id: 6,
          name: '蒸绿色蔬菜',
          calories: 172,
          preparation: '15分钟',
          image: 'https://images.unsplash.com/photo-1623428187969-5da2dcea5ebf?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
        }
      ]
    },
    
    // 推荐餐食
    recommendedMeals: [
      {
        id: 101,
        name: '蔬菜藜麦沙拉',
        description: '富含蛋白质和膳食纤维，低热量',
        calories: 210,
        image: 'https://images.unsplash.com/photo-1484980972926-edee96e0960d?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
        tags: ['低碳水', '素食']
      },
      {
        id: 102,
        name: '香草烤鸡胸肉',
        description: '高蛋白低脂肪，搭配蔬菜营养均衡',
        calories: 280,
        image: 'https://images.unsplash.com/photo-1504674900247-0877df9cc836?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
        tags: ['高蛋白', '低碳水']
      },
      {
        id: 103,
        name: '牛油果鸡蛋吐司',
        description: '健康脂肪和优质蛋白，能量充沛',
        calories: 240,
        image: 'https://images.unsplash.com/photo-1604908177453-7462950a6a3b?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
        tags: ['高蛋白']
      }
    ],
    
    // 识别到的食物
    recognizedFood: {
      name: '牛油果沙拉',
      calories: 245,
      image: 'https://images.unsplash.com/photo-1555939594-58d7cb561ad1?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
      nutrition: {
        protein: 5.8,
        fat: 21,
        carbs: 12.5,
        fiber: 4.6
      },
      healthTip: '牛油果富含健康脂肪和纤维，可以帮助您保持饱腹感更长时间。搭配少量坚果食用，能够提供更多的营养价值。'
    }
  }),
  
  getters: {
    // 计算总卡路里摄入
    totalCalories: (state) => {
      let total = 0
      
      // 计算早餐卡路里
      state.meals.breakfast.forEach(meal => {
        total += meal.calories
      })
      
      // 计算午餐卡路里
      state.meals.lunch.forEach(meal => {
        total += meal.calories
      })
      
      // 计算晚餐卡路里
      state.meals.dinner.forEach(meal => {
        total += meal.calories
      })
      
      return total
    },
    
    // 计算早餐总卡路里
    breakfastCalories: (state) => {
      return state.meals.breakfast.reduce((sum, meal) => sum + meal.calories, 0)
    },
    
    // 计算午餐总卡路里
    lunchCalories: (state) => {
      return state.meals.lunch.reduce((sum, meal) => sum + meal.calories, 0)
    },
    
    // 计算晚餐总卡路里
    dinnerCalories: (state) => {
      return state.meals.dinner.reduce((sum, meal) => sum + meal.calories, 0)
    },
    
    // 计算卡路里进度百分比
    caloriesProgress: (state) => {
      return Math.round((state.caloriesIntake / state.caloriesTarget) * 100)
    }
  },
  
  actions: {
    // 添加餐食
    addMeal(mealType, meal) {
      this.meals[mealType].push(meal)
      this.updateCaloriesIntake()
    },
    
    // 移除餐食
    removeMeal(mealType, mealId) {
      const index = this.meals[mealType].findIndex(meal => meal.id === mealId)
      if (index !== -1) {
        this.meals[mealType].splice(index, 1)
        this.updateCaloriesIntake()
      }
    },
    
    // 更新卡路里摄入
    updateCaloriesIntake() {
      this.caloriesIntake = this.totalCalories
    },
    
    // 设置已识别的食物
    setRecognizedFood(food) {
      this.recognizedFood = food
    }
  }
}) 